recylerView example kotlin with image

152

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
  
class CustomAdapter(private val mList: List<ItemsViewModel>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
  
    // create new views
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        // inflates the card_view_design view 
        // that is used to hold list item
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.card_view_design, parent, false)
  
        return ViewHolder(view)
    }
  
    // binds the list items to a view
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  
        val ItemsViewModel = mList[position]
  
        // sets the image to the imageview from our itemHolder class
        holder.imageView.setImageResource(ItemsViewModel.image)
  
        // sets the text to the textview from our itemHolder class
        holder.textView.text = ItemsViewModel.text
  
    }
  
    // return the number of the items in the list
    override fun getItemCount(): Int {
        return mList.size
    }
  
    // Holds the views for adding it to image and text
    class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
        val imageView: ImageView = itemView.findViewById(R.id.imageview)
        val textView: TextView = itemView.findViewById(R.id.textView)
    }
}

Comments

Submit
0 Comments

More Questions

kotlin date to stringtoast in kotlin zsh: command not found: adb
coroutines kotlin android dependency2d array in kotlin how to initialize mutable list kotlin
date format kotlinkotlin check if string contains android recyclerview scroll to bottom
on click in kotlinrandom string generator kotlin kotlin list add
kotlin onclicklistenerkotlin empty list kotlin filter not null
what is the difference between const and valkotlin print list text is behind BottomNavigationView
kotlinx coroutines dependencyhow to check internet connection in android programmatically kotlin recyclerview onclicklistener kotlin
kotlin check if edittext is emptycountdowntimer kotlin kotlin null safety
how to get date in kotlinkotlin repeat n times split string kotlin
Kotlin redundant FindViewByIdfile is not upload using retrofit in kotlin how to uppercase the first letter of a string in kotlin
how to make dialog transparent androidstartactivityforresult deprecated android kotlin remove name from an activity
button inside a recycle view in android in kotlindate to string kotlin while loop kotlin
kotlin variable possiblement nullregex find emails kotlin for decrement
kotlin filter map by keyadd kotlin parcelize plugin in new android studio list map by index and value kotlin
object vs companion object kotlinhow to get resource how to get data in mainActivity from a parcelable data class in kotlin
android navigation drawer not navigating with navcontrollerdrawable to bitmap android kotlin double tab exit button